#!/bin/bash

check() {
    # 检查 SSH 相关命令是否存在
    require_binaries sshd || return 1
    return 0
}

depends() {
    echo -n "kernel-network-modules "
    if ! dracut_module_included "network-legacy" && [ -x "$dracutsysrootdir/usr/libexec/nm-initrd-generator" ] ; then
        echo "network-manager"
    else
        echo "network-legacy"
    fi
    return 0
}

# called by dracut
installkernel() {
    return 0
}

network() {
    local _arch _i _dir

    inst_script "$moddir/netroot.sh" "/sbin/netroot"
    inst_simple "$moddir/net-lib.sh" "/lib/net-lib.sh"
    inst_hook pre-udev 50 "$moddir/ifname-genrules.sh"
    inst_hook cmdline 91 "$moddir/dhcp-root.sh"

    dracut_need_initqueue

}

install() {
    network

    inst_multiple /usr/sbin/sshd /usr/bin/ssh /usr/bin/ssh-keygen /usr/libexec/openssh/sshd-keygen
    inst_multiple /bin/bash /bin/sh /usr/bin/id /usr/sbin/chpasswd /usr/sbin/useradd /usr/sbin/groupadd /usr/sbin/groupdel
    inst_multiple /bin/env /usr/bin/lsblk /usr/sbin/lspci /usr/sbin/ethtool /usr/sbin/chpasswd
    inst_multiple /bin/tee /usr/sbin/agetty /usr/bin/su /bin/whoami /bin/who /bin/killall /bin/head /bin/tail
    inst_multiple /bin/lscpu /bin/users /bin/wc /bin/date /bin/df /bin/free /bin/tty
    inst_multiple /bin/basename /bin/dirname /bin/expr /bin/mktemp /bin/locale /bin/localedef /bin/localectl
    inst_multiple /usr/bin/dd /bin/touch /usr/bin/ssh-copy-id /usr/bin/scp
    inst_multiple /usr/sbin/ip /bin/ping /usr/sbin/ifconfig
    inst_multiple /usr/bin/vim /usr/bin/vimdiff
    inst_multiple /usr/libexec/grepconf.sh /bin/chown /bin/chmod
    inst_multiple /usr/sbin/netlink-user

    # 安装密码验证相关工具
    inst_multiple /usr/bin/passwd /bin/login /usr/sbin/faillock /usr/sbin/mkhomedir_helper /usr/sbin/pam_namespace_helper /usr/sbin/pam_tally
    inst_multiple /usr/sbin/pam_tally2 /usr/sbin/pam_timestamp_check /usr/sbin/pwhistory_helper /usr/sbin/unix_chkpwd /usr/sbin/unix_update
    inst_dir "/lib64/security"
    cp /lib64/security/*.so  ${initdir}/lib64/security/
    inst_dir "/etc/pam.d"
    cp /etc/pam.d/* ${initdir}/etc/pam.d/
    inst_dir "/etc/security"
    cp -a /etc/security/* ${initdir}/etc/security/
    inst_simple "/etc/deepin/dde.conf"
    inst_dir "/etc/profile.d/"
    cp /etc/profile.d/* ${initdir}/etc/profile.d/
    inst_simple "/etc/profile"

    
    inst_libdir_file "libpam*"

    # 安装必要的库文件
    inst_libdir_file "libnss_*" "libcrypto*" "libssl*"
    
    inst_libdir_file "libattr.so.1*" "libgpm.so.2*"

    inst_simple "/usr/share/locale/zh_CN/LC_MESSAGES/util-linux.mo"
    inst_dir "/usr/lib/locale"
    cp -a /usr/lib/locale/zh_CN.utf8 ${initdir}/usr/lib/locale/
    cp -a /usr/lib/locale/zh_CN ${initdir}/usr/lib/locale/
    
    inst_simple "/etc/bashrc"
    
    # 安装 SSH 配置文件
    inst_simple "/etc/ssh/sshd_config"
    inst_simple "/etc/ssh/ssh_config"
    inst_dir "/usr/share/licenses/pam"
    cp /usr/share/licenses/pam/* ${initdir}/usr/share/licenses/pam/
    
    cp "$moddir/issue.initramfs" ${initdir}/etc/issue.initramfs

    # 安装主机密钥（如果存在）
    for key in /etc/ssh/ssh_host_*_key; do
        [ -f "$key" ] && inst_simple "$key"
    done

    # 安装 PAM 配置（如果使用 PAM 认证）
    if [ -d "/etc/pam.d" ]; then
        inst_dir "/etc/pam.d"
        inst_simple "/etc/pam.d/*"
    fi
    inst_dir /etc/sysconfig
    inst_simple /etc/sysconfig/sshd
    inst_simple /etc/sysconfig/bash-prompt-xterm
    inst_dir /usr/share/locale/zh_CN
    cp -a /usr/share/locale/zh_CN/*  ${initdir}/usr/share/locale/zh_CN/

    # 安装 nsswitch 配置
    inst_simple "/etc/nsswitch.conf"

    # 创建必要的目录结构
    mkdir -p "${initdir}/etc/ssh"
    mkdir -p "${initdir}/var/empty/sshd"
    mkdir -p "${initdir}/root/.ssh"

    ## 配置ssh
    sed -i 's/PermitRootLogin.*/PermitRootLogin yes/g' ${initdir}/etc/ssh/sshd_config
    sed -i 's/PasswordAuthentication.*/PasswordAuthentication yes/g' ${initdir}/etc/ssh/sshd_config
    #sed -i 's/UsePAM.*/UsePAM no/g' ${initdir}/etc/ssh/sshd_config
    sed -i 's/.*Banner.*/Banner \/etc\/issue.initramfs/g' ${initdir}/etc/ssh/sshd_config

    rm -f ${initdir}/init
    cp "$moddir/init.sh"  ${initdir}/init
    cp "$moddir/ramfs.sh"  ${initdir}/sbin/ramfs.sh
    cp "$moddir/reboot-uos"  ${initdir}/sbin/reboot-uos

    mkdir -p ${initdir}/etc
    pw=$(openssl passwd -6 "1")
    echo "root:$pw:20321:0::7:::" > ${initdir}/etc/shadow
}

